<?php

/*************************************\

    Handshakes Professional (3.0)
            by DZOIC LTD
       Copyright(c) 2005-2006
        auth:zhangchaofeng

\*************************************/

class DB3 {

	var $conn_id = false;
	function DB3(){
		$this->values = array();
		$this->sqldb = "sigup";
		$this->sqlhost = "127.0.0.1";
		$this->sqluser = "root";
		$this->sqlpass = "123123";

	}//DB3

    function connect() {

        $GLOBALS["DB3"]->conn_id = mysql_pconnect($this->sqlhost, $this->sqluser, $this->sqlpass,1);
        mysql_select_db($this->sqldb,$GLOBALS["DB3"]->conn_id);
        $this->execute("SET NAMES 'utf8'");
        set_time_limit(0);
        /*$version = $this->version();
				
        if($version >= 4.1){
        	$encoding = extract_encoding("mysql");
        	if($encoding){
        		$sql_query = "SET NAMES '".$encoding."'";
        		$this->execute($sql_query);
        	}//if
        	$sql_query = "SET sql_mode = 'MYSQL40'";
        	$this->execute($sql_query);
        }//if
				*/
    }//connect

    function disconnect(){

    	if($GLOBALS["DB3"]->conn_id)
    		@mysql_close($GLOBALS["DB3"]->conn_id);

    }//disconnect

    function version(){

    	$sql_query = "SELECT VERSION()";
    	$version   = $this->single($sql_query);
    	$version   = ereg_replace("[^0-9|\.]","",$version);
    	return $version;

    }//version

    function execute($sql_query, $suppress_errors = 0) {

        if(!$GLOBALS["DB3"]->conn_id){

            $this->connect();

        }//if

        $sql_query = preg_replace("/\:\:/","|;|;",$sql_query);

        preg_match_all("/\:[A-Za-z|0-9|_]{1,}\:/",$sql_query,$matches);

        $patterns = $matches[0];

        while(list(,$val) = each($patterns)){
        	$key   = substr($val,1,strlen($val)-2);
        	$value = stripslashes($this->values[$key]);
        	$sql_query = hs_ereg_replace($val,mysql_escape_string($value),$sql_query);
        }//while

		reset($this->values);

		$sql_query = preg_replace("/\|;\|;/","::",$sql_query);

        $this->sql_result = mysql_query($sql_query,$GLOBALS["DB3"]->conn_id);

       if(!$suppress_errors && !$this->sql_result && !hs_ereg("^LOCK TABLES",$sql_query) && $sql_query!="UNLOCK TABLES"){
        	if( mysql_errno($GLOBALS["DB3"]->conn_id) == 2006 ) {
        		$this->connect();
        		$this->execute( $sql_query );
        	} else 
        		die($sql_query.":".mysql_error($GLOBALS["DB3"]->conn_id).":".mysql_errno($GLOBALS["DB3"]->conn_id));
        }//if

        return $this->sql_result;

    }//execute

    function insert($sql_query){

    	if((!$this->sql_result)||($sql_query != $this->sql_query)){
            $this->execute($sql_query);
        }//if

        $this->clean();
        return mysql_insert_id($GLOBALS["DB3"]->conn_id);

    }//insert

    function update($sql_query){

    	if((!$this->sql_result)||($sql_query != $this->sql_query)){
            $this->execute($sql_query);
        }//if

        $this->clean();
        return mysql_affected_rows($GLOBALS["DB3"]->conn_id);

    }//update

    function fetch($sql_query, $loop = 0){
    	
    	static $row = 0;

        if((!$this->sql_result)||($sql_query != $this->sql_query)){
            $this->execute($sql_query);
        }//if

        $this->sql_query = $sql_query;
        $result = @mysql_fetch_object($this->sql_result);
        
        if(!$result || !$loop)
        	$this->clean();

        return $result;

    }//fetch

    function row($sql_query, $loop = 0){

        if((!$this->sql_result)||($sql_query != $this->sql_query)){
            $this->execute($sql_query);
        }//if

        $this->sql_query = $sql_query;
        $result = @mysql_fetch_assoc($this->sql_result);
        
        if(!$result || !$loop)
        	$this->clean();

        return $result;

    }//row
    
    function result($sql_query){

    	$ret_array = array();

        while($this->res = $this->row($sql_query, 1)){
            $ret_array[] = $this->res;
        }//while

        @mysql_free_result($this->res);
        $this->clean();
        return $ret_array;

    }//result

    function single($sql_query){

        $this->execute($sql_query);
        $this->clean();
		$return = @mysql_result($this->sql_result,0);
        return $return;

    }//single

    function lock($table){

    	$sql_query="LOCK TABLES `".$table."` WRITE";
    	$this->execute($sql_query);

    }//lock

    function unlock(){

    	$sql_query="UNLOCK TABLES";
    	$this->execute($sql_query);

    }//unlock

    function clean(){

    	$this->sql_query = "";

    }//clean

}//DB3 class

$GLOBALS["DB3"] = new DB3;

?>
